define(function(require, exports, module) {
	
	require("ztree");
	
	module.exports = function() {
		var zTree = null;
		var params = {};
		var row = {};
		return {
			data: {
				title:$.i18n.prop("sys.router.manager.perm"),
				button:{
					submit: $.i18n.prop("sys.submit")
				}
			},
			methods: {
				init: function(data) {
					var me = this;
					params = data;
					row = data.row;
					this.initTree(function() {me.loading("close");});
					return false;
				},
				//初始化权限树
				initTree:function(complete) {
					var me = this;
					var ul = $(this.$el).find("ul");
					var setting = {
						data : {
							simpleData: {
								enable: true,
								pIdKey: "parent_id"
							}
						},
						check : {
							enable : true,
							chkStyle : "radio",
							radioType : "all",
							chkboxType : { "Y": "", "N": ""}
						}
					};
					$.post("/api/router/tree", {}, function(resp) {
						if(resp.success) {
							var rootNode = {
								id:"root",
								name:$.i18n.prop("sys.select", [$.i18n.prop("sys.router.manager.perm")]),
								open:true,
								nocheck:true,
								children:resp.message
							};
							zTree = $.fn.zTree.init(ul, setting, rootNode);
							if(row.permId) {
								var node = zTree.getNodeByParam("id", row.permId, null);
								zTree.checkNode(node, true, false, false);
							}
							if(typeof(complete) == "function") {
								complete();
							}
						}
					}, "json");
				},
				submit : function() {
					this.loading();
					var items = zTree.getCheckedNodes(true);
					var permId = "";
					var me = this;
					if(items.length > 0) {
						permId = items[0].id;
					}
					$.post("/api/router/perm", {
						id:row.id,
						permId:permId
					}, function(data) {
						row.permId = permId;
						params.update(row);
						me.loading("close");
						me.closeWindow();
					}, "json");
				},
				destroy: function() {
					zTree.destroy();
				}
			}
		}
	}
});